<?php 
include('includes/confirm.php');
$page_name = media;
require_once 'classes/Mysql.php';
//require_once 'includes/constants.php';
$mysql = New Mysql();
$message="";

if($_POST && isset($_POST['delete'])){
		
		$stmt4=$mysql->run_query("DELETE FROM resource_copy WHERE id=?", $_POST['delete']);
		$message="Deleted Copy!!";
		
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <?php include('includes/head.php'); ?>
<body>
<div id="wrapper">
    <?php include('includes/header.php'); ?>
    <?php include('includes/menu.php'); ?>
    
    <div id="page">
    	
	      <div id="content">
				<?php
					echo "<br>$message<br>";
				?>
			<div id="browse_result">
				<form method="post" action="edit_resource.php">
				<input type="submit" name="edit_resource" value="   Edit   "><br><br>
				<table class="sortable" id=anyid cellpadding="0" cellspacing="0">
					<tr>
						<th class="unsortable">Resource Info</th>
						<th class="unsortable">Details</th>
					</tr>
			<?php
				 if(isset($_POST['delete']))
				{
							//echo "<h4>sdfsdfsadkfjsakdlf</h4>";
							//$stmt4=$mysql->run_query("DELETE FROM resource_copy WHERE id=?", $_POST['delete']);
							//$message="Deleted Copy!!";
							//echo "jksdfkjhsafjsdafkhsdkjfhlsakjdf";
							$resource_id = $_POST['resource_id'];
				}
				if($_POST && isset($_POST['resource_id'])){
					$resource_id = $_POST['resource_id'];
					switch($_POST['type_id']){
						case 1: // movies
							$stmt=$mysql->run_query("UPDATE resource r, movie m
																				SET r.title=?, r.year=?, m.length=?,  m.imdb_link=?, m.rating=?
																				WHERE r.id=m.id
																				AND r.id=?", $_POST['title'], $_POST['year'], $_POST['length'], $_POST['imdb_link'], $_POST['rating'], $resource_id);
																				$stmt->close();
							break;
						case 2: // book
							$stmt= $mysql->run_query("UPDATE resource r, book b
																				SET r.title=?, r.year=?, b.publisher=?,  m.isbn=? 
																				WHERE r.id=b.id
																				AND r.id=?", $_POST['title'], $_POST['year'], $_POST['publisher'], $_POST['isbn'], $resource_id);
																				$stmt->close();
							break;
						case 3: // album
							$stmt= $mysql->run_query("UPDATE resource r, album a
																				SET r.title=?, r.year=?, a.no_of_tracks=?
																				WHERE r.id=a.id
																				AND r.id=?", $_POST['title'], $_POST['year'], $_POST['no_of_tracks'], $resource_id);
																				$stmt->close();
							break;
						case 4: //videogames
							$stmt=$mysql->run_query("UPDATE resource r, videogame v
																				SET r.title=?, r.year=?, v.company=?,  v.rating=?, m.console=?
																				WHERE r.id=v.id
																				AND r.id=?", $_POST['title'], $_POST['year'], $_POST['company'], $_POST['rating'], $_POST['console'], $resource_id);
																				$stmt->close();
							break;
							
					}
				}
				if($_GET && !empty($_GET['resource_id'])){
					$resource_id=$_GET['resource_id'];
				}
					print '<input type="hidden" name="resource_id" value=';
					print $resource_id;
					print " >";
					
					$stmt=$mysql->run_query("SELECT type_id FROM resource WHERE id=?", $resource_id);
					$stmt->bind_result($type_id);
					$stmt->fetch();
					$stmt->close();
					switch($type_id){
						case 1: //movie
								$stmt=$mysql->run_query("SELECT r.title, r.year, m.length, m.imdb_link, m.rating
																					FROM resource r, movie m
																					WHERE r.id=m.id
																					AND r.id=?",$resource_id);
								$stmt->bind_result($title, $year, $lenght, $imdb_link, $rating);
								$stmt->fetch();
								$stmt->close();
								print "<tr><td>Title</td>			<td>$title</td>		</tr>";
								print "<tr><td>Year</td>			<td>$year</td>		</tr>";
								print "<tr><td>Lenght</td>		<td>$lenght</td>	</tr>";
								print "<tr><td>IMDB Link</td>		<td><a href=$imdb_link>$imdb_link</a></td>	</tr>";
								print "<tr><td>Rating</td>		<td>$rating</td>	</tr>";
								break;
						case 2: //book
								$stmt=$mysql->run_query("SELECT r.title, r.year, b.publisher, b.isbn
																					FROM resource r, book b
																					WHERE r.id=b.id
																					AND r.id=?",$resource_id);
								$stmt->bind_result($title, $year, $publisher, $isbn);
								$stmt->fetch();
								$stmt->close();
								print "<tr><td>Title</td>			<td>$title</td>		</tr>";
								print "<tr><td>Year</td>			<td>$year</td>		</tr>";
								print "<tr><td>Publisher</td>	<td>$publisher</td>	</tr>";
								print "<tr><td>ISBN</td>			<td>$isbn</td>	</tr>";
								break;
						case 3: //album
								$stmt=$mysql->run_query("SELECT r.title, r.year, a.no_of_tracks
																					FROM resource r, album a
																					WHERE r.id=a.id
																					AND r.id=?",$resource_id);
								$stmt->bind_result($title, $year, $no_of_tracks);
								$stmt->fetch();
								$stmt->close();
								print "<tr><td>Title</td>			<td>$title</td>		</tr>";
								print "<tr><td>Year</td>			<td>$year</td>		</tr>";
								print "<tr><td>No. of tracks</td>		<td>$no_of_tracks</td>	</tr>";
								break;
						case 4: // videogame
								$stmt=$mysql->run_query("SELECT r.title, r.year, v.company, v.rating, v.console
																					FROM resource r, videogame v
																					WHERE r.id=v.id
																					AND r.id=?",$resource_id);
								$stmt->bind_result($title, $year, $company, $rating, $console);
								$stmt->fetch();
								$stmt->close();
								print "<tr><td>Title</td>			<td>$title</td>		</tr>";
								print "<tr><td>Year</td>			<td>$year</td>		</tr>";
								print "<tr><td>Company</td>		<td>$company</td>	</tr>";
								print "<tr><td>Rating</td>		<td>$rating</td>	</tr>";
								print "<tr><td>Console</td>		<td>$console</td>	</tr>";
								break;
						
					}
					$show_copies=true;
				
			?>
			</table><br>
			</form>
			<form method="post" action="add_copy.php">
			<?php
			echo '<input type="hidden" name="resource_id" value="'.$resource_id.'">';
			echo '<input type="hidden" name="type_id" value="'.$type_id.'">';
			?>
			<input type="submit" name="add_copy" value="   Add My Copy   ">
			</form>
			<br><br>
			
			<h3> Copies: </h3>
			<table class="sortable" id=anyid cellpadding="0" cellspacing="0">
				<tr>
					<th>Owner</th>
					<th>Condition</th>
					<th>Format</th>
					<th>Location</th>
					<th>Borrow</th>
			<?php
			if($show_copies==true){
					$stmt=$mysql->run_query("SELECT c.id, c.condition, f.name, u.uname, c.location, c.u_id
																	FROM resource_copy c, format f, user u
																	WHERE c.format_id=f.id 
																				AND c.u_id=u.id
																				AND c.resource_id=?
																				AND c.u_id IN (
																					SELECT cc.u_id 
																					FROM resource_copy cc
																					WHERE cc.u_id IN (SELECT id1 FROM friend WHERE id2=? AND c.share_level <= view_setting)
																					OR cc.u_id=?)", $resource_id, $_SESSION['userid'], $_SESSION['userid']);
					$stmt->bind_result($c_id, $condition, $format, $owner, $location, $u_id);
					
					while($stmt->fetch()){
						print "<tr>";
						print "<td>$owner</td><td>$condition</td><td>$format</td><td>$location</td>";
						print '<td>';

							$mysql2 = new Mysql();
							$query = "SELECT c.id FROM resource_copy c WHERE c.id ='".$c_id."' ";
							$query .= " AND c.u_id IN (SELECT id1 FROM friend WHERE id2='".$_SESSION['userid']."' AND c.share_level <= borrow_setting ) ";
							$stmt2 = $mysql2->run_query($query);
							$stmt2->bind_result($cc_id);
							if($stmt2->fetch()){							
								print '<form method="post" action="borrow_copy.php">';
								print "<input type=hidden name='borrow' value='$cc_id' />";
								print '<input type="submit" name="borrow_button" value="Borrow"/>';
								print '</form>';
							}
							
							
							if($u_id == $_SESSION['userid']){
							echo "c_id=$c_id";
								print '<form method="post" action="view_resource.php">';
								print "<input type=hidden name='delete' value='$c_id' />";
								print "<input type=hidden name='resource_id' value='$resource_id' />";
								print '<input type="submit" name="borrow_button" value="Delete"/>';
								print '</form>';
							}
						$stmt2->close();
						print '</td>';
						print "</tr>";
					}
					$stmt->close();
			}
			?>
			</table>
			<br>
			</div>
	      </div>
	      <!-- end div#content -->
	      <div id="sidebar">
			
	      </div>
	      <!-- end div#sidebar -->
	      <div style="clear: both; height: 1px"></div>
	   
	  </div>
  
  	</div>
  	<!-- end div#page -->
    <?php include('includes/footer.php'); ?>
</div> <!-- end div#wrapper -->
</body>
</html>
